<template>
  <div>
    <slot />
  </div>
</template>

<script lang="ts" setup>
const fields = ref({})

const validate = (tag = '') => {
  const validateFields = []
  for (const i in fields.value) {
    const field = fields.value[i] || null

    if (field) {
      const fieldTag = `${field.tag || ''}`
      if (fieldTag === tag) {
        validateFields.push(field.validate())
      }
    }
  }

  if (validateFields.length > 0) {
    return Promise.all(validateFields).then((args = []) => {
      let result = true

      for (const arg of args) {
        if (!arg) {
          result = false
          break
        }
      }

      if (result) {
        return true
      } else {
        throw false // eslint-disable-line
      }
    })
  } else {
    return Promise.resolve(true)
  }
}
const registerField = (target: { _uid: any; $options: { componentName: any; }; }) => {
  if (target && target._uid) {
    const uid = target._uid
    const componentName = target.$options.componentName

    if (componentName === 'ElFormItem') {
      $set(fields.value, uid, target)
    }
  }
}
const unregisterField = (target: { _uid: any; }) => {
  if (target && target._uid) {
    const uid = target._uid
    $delete(fields.value, uid)
  }
}
 provide('registerField',registerField )
 provide('unregisterField',unregisterField )
</script>
